import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public class SimpleNetty {
    public static void main(String[] args) {
        new ServerBootstrap()
                //2.  BossEventLoop, WorkerEventLoop(selector,thread), group 组
                .group(new NioEventLoopGroup())
                //3.选择 服务器的 ServerSocketChannel 实现
                .channel(NioServerSocketChannel.class)
                //4.boss负责处理连接worker(child)负责处理读写,决定了worker(child)能执行哪些操作(handler)
                .childHandler(
                 // 5.channel 代表和客户端进行数据读写的通道 Initializer 初始化，负责添加别的 handler
                    new ChannelInitializer<NioServerSocketChannel>() {
                        @Override
                        protected void initChannel(NioServerSocketChannel ch) throws Exception {
                            // 添加具体的handle
                            ch.pipeline().addLast(new StringDecoder());//将bytebuf转换成string
                            ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){// 自定义handle
                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    //                                ctx.fireChannelRead(msg);
                                    System.out.println(msg);
                                }
                            });
                        }
                })
                .bind(8080);
    }
}
